﻿<%@ Page Title="Tìm kiếm" Language="C#" MasterPageFile="~/Masters/MasterPage.master" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
<%@ Register src="Parts/TopProducts.ascx" tagname="TopProducts" tagprefix="uc3" %>
<%@ Register src="Parts/Adv.ascx" tagname="Adv" tagprefix="uc4" %>
<%@ Import Namespace="System.Linq.Dynamic" %>

<asp:Content ID="Content1" ContentPlaceHolderID="title" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentBoxFloatRightPlaceHolder" Runat="Server">

<% 
   
    int curpage = 1;
    if (Request.QueryString["Page"] != null)
        curpage = Convert.ToInt32(Request.QueryString["Page"].ToString());       
    int perpage = 10;
    
    string cateid = "";
    string cateName = Request.Form["CateName"];
    StringBuilder cateWhere = new StringBuilder();
    if (cateName != null)
    {       
        string[] cateNames = cateName.Split(',');
        foreach (string item in cateNames)
        {   
            if(cateWhere.ToString()!="")
                cateWhere.AppendFormat(" or CatID == \"{0}\" ", item.Trim());
            else
                cateWhere.AppendFormat("CatID == \"{0}\" ", item.Trim());
        }
    }
    string keyWord = "";
    if (Request.Form["searchInputTop"] != null)
    {
        keyWord = Request.Form["searchInputTop"];
        if(cateWhere.ToString().Trim()!="")
             cateWhere.AppendFormat(" and ProductName == \"%{0}%\"", keyWord);
        else
            cateWhere.AppendFormat(" ProductName == \"%{0}%\"", keyWord);
        
    }
    try
    {
        cateid = Request.QueryString["CateID"].ToString();
        curpage = int.Parse(Request.QueryString["Page"].ToString());
    }
    catch { }

    NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();        

    var query = from sp in db.TaiLieus
                where sp.CatID.Equals(cateid)
                select sp;
    //.Where("CatID == @0 and ProductName == @1","1","%a%");
    if (cateWhere.ToString() != "")
        query = db.TaiLieus
                .Where(cateWhere.ToString());
                
    if (query.Count<TaiLieu>() == 0)
    {
        query = from sp in db.TaiLieus select sp;
    }

    int nums = query.Count<TaiLieu>();
    int numspage = nums / perpage;
    if (nums % perpage != 0)
    {
        numspage++;
    }

    int skip = (curpage - 1) * perpage;

    List<TaiLieu> lst = query.Skip(skip).Take(perpage).ToList<TaiLieu>();
        
    int nextpage = curpage + 1;
    if (curpage == numspage)
    {
        nextpage = 1;
    }    
%>
    <div class="toolbar search-toolbar clearfix">
    	<div class="left"></div>
    	<div class="right"></div>
    	<div class="center">
    		<div class="title">
                Tìm thấy: <strong><%= lst.Count %></strong> kết quả
            </div>                
    	</div>
    </div>

    <div class="sort-and-pages">	 
		<div class="paging">
    			<ul class="clearfix">

                    <li class="last">  
                        <a href="Category.aspx?CateID=<%= cateid %>&Page=<%= nextpage %>">Next ››</a>
                    </li>
<%
    for (int i = numspage; i > 0; i--)
    {
        string current = "";
        if (i == curpage)
        {
            current = "current";
        }
%>
                    <li class="<%= current %>">
                        <a href="Category.aspx?CateID=<%= cateid %>&Page=<%= i %>"><%= i %></a>
                    </li>
<%
    } 
%>
					<li class="title">Page</li>

                </ul>
		</div>
    </div>

    <!--begin: 2cols  -->
    <div class="2cols">
    <!--begin: results  -->
	    <div id="results">

			<span id="lblResults">
                <ul>

<% 
    for (int i = 0; i < lst.Count; i++)
    {
%>

<li class="search-row clearfix">
    <div class="img">
        <a href="Product.aspx?ID=<%= lst[i].ProductID %>">
            <img width="71" height="83" onload="return '<%= lst[i].ProductPhotoURL %>'" 
                class="img-lazy-load" src="<%= lst[i].ProductPhotoURL %>" 
                alt="<%= lst[i].ProductName %>" title="<%= lst[i].ProductName %>" />
        </a>
    </div>

    <div class="descr">
        <h4>
            <a href="Product.aspx?ID=<%= lst[i].ProductID %>"><%= lst[i].ProductName %></a>
        </h4>

        <div class="additional-info">
            <span><%= lst[i].ProductPrice + " USD" %></span> 
            <%--<span>2012;</span> 
            <span><span>US$ 24.99</span></span>--%>
        </div>
        <p> 
            <% 
                if (lst[i].ProductDescription.Length > 270)
                {
                    lst[i].ProductDescription = lst[i].ProductDescription.Substring(0, 270) + "...";
                }
            %>
            <%= lst[i].ProductDescription %>
            <a href="Product.aspx?ID=<%= lst[i].ProductID %>">Xem thêm</a>
        </p>
    </div>
</li>
<% 
    }
%>

                </ul>
            </span>
                    
            <div class="paging bottom floatLeft">
    	        <ul class="clearfix">

                    <li class="last">  
                        <a href="Category.aspx?CateID=<%= cateid %>&Page=<%= nextpage %>">Next ››</a>
                    </li>
<%
    for (int i = numspage; i > 0; i--)
    {
        string current = "";
        if (i == curpage)
        {
            current = "current";
        }
%>
                    <li class="<%= current %>">
                        <a href="Category.aspx?CateID=<%= cateid %>&Page=<%= i %>"><%= i %></a>
                    </li>
<%
    } 
%>
					<li class="title">Page</li>

                </ul>
            </div>
			
        </div><!-- end: results -->

       <!--begin: rightSide  -->
       <div class="rightSide">

            <!--begin: Top Products  -->
            <uc3:TopProducts ID="TopProducts1" runat="server" />
            <!-- end: Top Products -->

			<!-- begin:Adv -->
            <uc4:Adv ID="Adv1" runat="server" />
            <!-- end: Adv -->    
    
       </div> 
       <!-- end: rightSide -->        
    </div>
    <!-- end: 2cols -->

</asp:Content>